﻿using Future.IDAL;
using Future.Model.DtoModel;
using Future.Model.SysModel;
using System.Text;

namespace Future.DAL
{
    /// <summary>
    /// 描述：菜单数据访问类
    /// 创建人： yangkai
    /// 创建时间：2022/11/15 22:02:22
    /// </summary>
    public class MenuDAL : BaseDAL<MenuModel>, IMenuDAL
    {
        public List<MenuModel> GetMenuList(MenuQueryDto menuQueryDto)
        {
            string sql = @"SELECT [Id]
                              ,[MenuName]
                              ,[ParentId]
                              ,[OrderNum]
                              ,[Url]
                              ,[IsLink]
                              ,[Target]
                              ,[MenuType]
                              ,[Visible]
                              ,[IsRefresh]
                              ,[PermissionCode]
                              ,[Icon]
                              ,[Router]
                              ,[Component]
                              ,[Query]
                              ,[CreateBy]
                              ,[CreateTime]
                              ,[UpdateBy]
                              ,[UpdateTime]
                              ,[Remark]
                          FROM [Future].[dbo].[Sys_Menu]
                          WHERE 1 = 1";

            StringBuilder stringBuilder = new();
            Dictionary<string, object> parameters = new();
            if (!string.IsNullOrEmpty(menuQueryDto.MenuName))
            {
                stringBuilder.Append(" and MenuName = @MenuName");
                parameters.Add("MenuName", menuQueryDto.MenuName);
            }

            if (menuQueryDto.Visible.HasValue)
            {
                stringBuilder.Append(" and Visible = @Visible");
                parameters.Add("Visible", menuQueryDto.Visible.Value);
            }

            sql += stringBuilder.ToString();
            return GetList(sql, parameters);
        }

        public List<MenuModel> GetMenusByUserId(Guid userId)
        {
            string sql = @"select distinct Sys_Menu.*
                            from Sys_UserRole
                            join Sys_RoleMenu on Sys_UserRole.RoleId = Sys_RoleMenu.RoleId
                            join Sys_Menu on Sys_Menu.Id = Sys_RoleMenu.MenuId
                            where Sys_UserRole.UserId = @UserId";
            Dictionary<string, object> parameters = new()
            {
                { "UserId", userId }
            };
            return GetList(sql, parameters);
        }
    }
}
